// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Linq;
using NUnit.Framework;
using ValidationAspects;
using ValidationAspects.Exceptions;

namespace TestValidationAspects.Exceptions
{
	[TestFixture]
	public class TestValidationContextException
	{
		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void Test_Constructor_throws_for_null_param()
		{
			new ValidationContextException(null);
		}

		[Test]
		public void Test_Message_returns_message_of_first_Exception()
		{
			ValidationException exception = new ValidationException("message");
			Assert.AreSame("message", new ValidationContextException(new[] {exception}).Message);
		}

		[Test]
		public void Test_Message_for_empty_Exceptions_is_harmless()
		{
			string message = new ValidationContextException(Enumerable.Empty<ValidationException>()).Message;
		}

		[Test]
		public void Test_Messages()
		{
			ValidationException[] exceptions = new[] {new ValidationException("message1"), new ValidationContextException(new[] {new ValidationException("message2")})};
			string[] messages = new ValidationContextException(exceptions).Messages.ToArray();
			Assert.AreEqual(2, messages.Count());
			Assert.AreEqual("message1", messages[0]);
			Assert.AreEqual("message2", messages[1]);
		}

		[Test]
		public void Test_Exceptions_returns_provided_nested_exceptions()
		{
			ValidationException[] exceptions = new[] {new ValidationException(), new ValidationContextException(new[] {new ValidationException()})};
			Assert.AreSame(exceptions, new ValidationContextException(exceptions).Exceptions);
		}

		[Test]
		public void Test_Flatten_returns_flattened_provided_nested_exceptions()
		{
			ValidationException exception1 = new ValidationException();
			ValidationException exception2 = new ValidationException();

			ValidationException[] exceptions = new[] {exception1, new ValidationContextException(new[] {exception2})};
			ValidationException[] flattened = new ValidationContextException(exceptions).Flatten().ToArray();

			Assert.AreEqual(2, flattened.Length);
			Assert.AreSame(exception1, flattened[0]);
			Assert.AreSame(exception2, flattened[1]);
		}
	}
}